//
//  SMSRequestProvider.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/26.
//


#import "LocationRequestProvider.h"
#import "LocationRequest.h"
#import <CoreLocation/CoreLocation.h>

@implementation TrackLocationModel

+ (void)location:(void(^)(TrackLocationModel *model))completion {
    // 纬度
    CLLocationDegrees latitude = [NSUserDefaults.standardUserDefaults doubleForKey:@"latitude"];
    // 经度
    CLLocationDegrees longitude = [NSUserDefaults.standardUserDefaults doubleForKey:@"longitude"];
    
    // 创建CLLocation对象
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    // 创建CLGeocoder对象
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 进行逆地理编码
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error) {
            !completion ?: completion(nil);
            NSLog(@"逆地理编码失败: %@", error.localizedDescription);
            return;
        }
        
        if (placemarks && placemarks.count > 0) {
            CLPlacemark *placemark = placemarks.firstObject;
            
            NSString *country = placemark.country?:@""; // 国家
            NSString *countryCode = placemark.ISOcountryCode?:@"";
            // 获取省市区信息
            NSString *administrativeArea = placemark.administrativeArea ?:@""; // 省
            NSString *locality = placemark.locality?:@""; // 市
            NSString *subLocality = placemark.subLocality?:@""; // 区
            
            NSLog(@"省: %@", administrativeArea);
            NSLog(@"市: %@", locality);
            NSLog(@"区: %@", subLocality);
            
            TrackLocationModel *model = [TrackLocationModel new];
            model.country = country;
            model.countryCode = countryCode;
            model.province = administrativeArea;
            model.city = locality;
            model.street = placemark.thoroughfare;
            !completion ?: completion(model);
        } else {
            NSLog(@"未找到对应的地理信息");
            !completion ?: completion(nil);
        }
    }];
}
@end




@implementation LocationRequestProvider

+ (void)requestTrackWithLocation:(TrackLocationModel *)model success:(void(^)(NSDictionary *dict))success failure:(void(^)(YTKRequest *request))failure {
    LocationRequest *request = [LocationRequest new];
    request.witticisms = model.countryCode;
    request.happiest = model.country;
    request.paralyzed = model.province;
    request.portion = model.city;
    request.nicknames = model.street;
#ifdef DEBUG
//    request.witticisms = @"CN";
//    request.happiest = @"China";
//    request.paralyzed = @"北京市";
//    request.portion = @"北京市";
//    request.nicknames = @"天安门广场";
#endif
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        if (![dict isKindOfClass:NSDictionary.class]) {
            !failure ?: failure(request);
            return;
        }
        if ([dict[@"weeps"] intValue] == 0) {
            // 成功
            !success ?: success(dict[@"bounding"]);
        } else {
            !failure ?: failure(request);
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        !failure ?: failure(request);
    }];
}



@end
